package com.guigu.ababa.ababaclassroom.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {

    public static final String COOKIE_KEY = "LOGIN_COOKIE";

    public static String getLoginCookie(HttpServletRequest request, HttpServletResponse response, boolean isCreate){

        for (Cookie cookie:request.getCookies()){
            String name =cookie.getName();
            if (COOKIE_KEY.equals(name)){
                if(cookie.getValue()!=null){
                    System.out.println(cookie.getValue());
                    return cookie.getValue();
                }else {
                    if (!isCreate){
                        return null;
                    }
                }
            }
        }
        String UUID = java.util.UUID.randomUUID().toString();

        Cookie cookie =new Cookie(COOKIE_KEY,UUID);
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
        return UUID;
    }
}
